package com.vt.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vt.admin.api.dto.user.*;
import com.vt.admin.api.dto.user.structure.UserOrganStructure;
import com.vt.admin.api.entity.SysUser;
import com.vt.admin.service.SmsValidateService;
import com.vt.admin.service.SysUserService;
import com.vt.common.core.constant.SecurityConstants;
import com.vt.common.core.constant.enums.EnumLoginType;
import com.vt.common.core.util.R;
import com.vt.common.security.util.SecurityUtils;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();
    @Autowired
    private SysUserService userService;
    @Autowired
    private SmsValidateService smsValidateService;

    /**
     * 获取当前用户信息（角色、权限）
     *
     * @param from     请求标志，该接口会被 auth、 前端调用
     * @param username 用户名
     */
    @ApiOperation(value = "", hidden = true)
    @GetMapping(value = {"/info", "/info/{username}"})
    public R<UserInfo> user(@PathVariable(required = false) String username,
                            @RequestHeader(required = false) String from) {
        // 查询用户不为空时判断是不是内部请求
        if (StrUtil.isNotBlank(username) && !StrUtil.equals(SecurityConstants.FROM_IN, from)) {
            return new R(null, "error");
        }
        //为空时查询当前用户
        if (StrUtil.isBlank(username)) {
            username = SecurityUtils.getUser().getUsername();
        }

        return new R(userService.findUserInfo(EnumLoginType.PWD.getType(), username));
    }

    /**
     * 获取当前用户信息（角色、权限）
     *
     * @param mobile 手机号码
     */
    @ApiOperation(value = "", hidden = true)
    @GetMapping(value = "/mobile/{mobile}")
    public R<UserInfo> mobile(@PathVariable("mobile") String mobile) {
        return new R(userService.findUserInfo(EnumLoginType.MOBILE.getType(), mobile));
    }

    @ApiOperation("获取当前用户信息（简短的）")
    @GetMapping("/vUserInfo")
    public R<VueUserInfo> getUserInfo() {
        return new R(userService.createVInfo());
    }

    @ApiOperation("用户权限数据")
    @GetMapping("/getCurrentUserAuthority")
    public R<UserAuthorities> currentUserAuthority() {
        return new R(userService.currentUserAuthority());
    }

    @ApiOperation("用户的组织、职位、角色数据")
    @GetMapping("/getUserOrganStructure")
    public R<UserOrganStructure> userOrganStructureInfo(@RequestParam String id) {
        return new R(userService.userOrganStructureInfo(id));
    }

    @ApiOperation("用户分页查询")
    @GetMapping
    public IPage<SysUserFullDto> list(SysUserQueryParam param) {
        return userService.list(param);
    }

    @ApiOperation("根据ID获取SysUser")
    @GetMapping("/id/{id}")
    public R<SysUser> getById(@PathVariable("id") String id) {
        return new R(userService.getById(id));
    }

    @PostMapping
    public R<Boolean> save(@RequestBody SysUserFullDto user) {
        return userService.save(user);
    }

    @PostMapping("login_sms")
    public R<Boolean> sendSms(@RequestBody String phone) {
        return smsValidateService.sendLoginSms(phone);
    }

    @PostMapping("resetPwd")
    public R<Boolean> resetPwd(@RequestBody SysUser user) {
        return userService.resetPwd(user);
    }

    @ApiModelProperty("登出")
    @RequestMapping("logout")
    public R<Boolean> logout(@RequestHeader(value = "Authorization", required = false) String authHeader) {
        return userService.logout(authHeader);
    }

    @ApiModelProperty("启用和禁用")
    @PostMapping("updateState")
    public R<Boolean> updateState(@RequestBody SysUser t){
        return userService.updateState(t);
    }
}
